package com.dodo.blog.ui.page.admin;

import com.dodo.blog.model.Playground;
import com.dodo.blog.model.Role;
import com.dodo.blog.request.BasicRequest;
import com.dodo.blog.server.PlaygroundService;
import com.dodo.blog.ui.ajax.Response;
import com.dodo.blog.ui.ajax.annotation.AjaxRequest;
import com.dodo.blog.ui.authorization.Authorized;
import com.dodo.blog.ui.component.input.Anchor;
import com.dodo.blog.ui.component.input.Button;
import com.dodo.blog.ui.component.repeater.IDataSource;
import com.dodo.blog.ui.component.repeater.OrderDirection;
import com.dodo.blog.ui.component.repeater.Table;
import com.dodo.blog.ui.component.repeater.TableHeader;
import com.dodo.blog.ui.page.AbstractPage;
import com.dodo.blog.ui.util.FormatUtil;

import javax.inject.Inject;
import java.util.List;

/**
 * @author <a href="mailto:pohorelec@comvai.com">Jozef Pohorelec</a>
 */
@Authorized( roles = {Role.ADMIN} )
public class PlaygroundList
        extends AbstractPage
{
    private static final long serialVersionUID = 1L;

    private static PlaygroundService playgroundService;

    private static final String DELETE_PLAYGROUND_METHOD = "deletePlayground";

    @AjaxRequest( name = DELETE_PLAYGROUND_METHOD )
    @SuppressWarnings( value = "unused" )
    public Response deletePlayground()
    {
        playgroundService.deletePlayground( getLongParameter( PlaygroundEdit.PLAYGROUND_ID ) );

        return Response.redirect( PlaygroundList.class );
    }

    @Override
    public void onRender()
    {
        setHeader( localize( "header.playgroundList" ) );

        Table<Playground> table = new Table<Playground>( "playground-list-table", new PlaygroundDataSource() )
        {
            private static final long serialVersionUID = 1L;

            @Override
            public TableHeader[] getTableHeaders()
            {
                return new TableHeader[]{
                        new TableHeader( "description", localize( "label.description" ) ),
                        new TableHeader( "createdDate", localize( "label.createdDate" ) ),
                        new TableHeader( "updatedDate", localize( "label.updatedDate" ) ),
                        new TableHeader(),
                };
            }

            @Override
            public void populate( Playground playground )
            {
                addColumn( playground.getDescription() );
                addColumn( playground.getCreatedDate(), FormatUtil.DATE_FORMAT_MONTH_YEAR );
                addColumn( playground.getUpdatedDate(), FormatUtil.DATE_FORMAT_MONTH_YEAR );
                addColumn(
                        new Anchor( "edit", localize( "button.edit" ), PlaygroundEdit.class ).addParameter( PlaygroundEdit.PLAYGROUND_ID, playground.getId() ),
                        new Button( "delete", playground.getId().toString(), localize( "button.delete" ) ).setAjaxMethod( DELETE_PLAYGROUND_METHOD )
                );
            }
        };
        add( table );

        add( new Anchor( "new-playground", localize( "button.new" ), PlaygroundEdit.class ) );
    }

    @Inject
    public void setPlaygroundService( PlaygroundService playgroundService )
    {
        PlaygroundList.playgroundService = playgroundService;
    }

    private class PlaygroundDataSource
            implements IDataSource<Playground>
    {
        private static final long serialVersionUID = 1L;

        @Override
        public List<Playground> getRows( int firstRow, int maxRows, String orderBy, OrderDirection direction )
        {
            BasicRequest request = new BasicRequest( firstRow, maxRows );
            request.addSort( orderBy, direction );
            return playgroundService.getPlaygroundList( request );
        }
    }
}